/**
 * jims
 */
package com.jims.master.modules.blood.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.blood.entity.BloodCapacity;
import com.jims.master.modules.blood.entity.BloodInpDetail;
import com.jims.master.modules.blood.entity.BloodMatch;
import com.jims.master.modules.blood.service.BloodImportService;
import com.jims.master.modules.blood.service.BloodMatchService;
import com.jims.master.modules.blood.vo.BloodMatchOutSheetVo;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.SysOrgDict;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.SysOrgDictService;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 配血功能Controller
 * @author fengyg
 * @version 2017-04-12
 */
@Controller
@RequestMapping(value = "${adminPath}/blood/bloodMatch")
public class BloodMatchController extends BaseController {

	@Autowired
	private BloodMatchService bloodMatchService;
    @Autowired
    private OrgDeptDao deptDao;
    @Autowired
    private BloodImportService bloodImportService;
    @Autowired
    private SysOrgDictService sysOrgDictService;

	@ModelAttribute
	public BloodMatch get(@RequestParam(required=false) String id) {
		BloodMatch entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bloodMatchService.get(id);
		}
		if (entity == null){
			entity = new BloodMatch();
		}
		return entity;
	}

    /**
     * 配血首页
     * @param bloodMatch
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(BloodMatch bloodMatch, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (bloodMatch.getStartDate()==null){
            bloodMatch.setStartDate(new Date());
        }
        if (bloodMatch.getStopDate() == null) {
            bloodMatch.setStopDate(new Date());
        }
        bloodMatch.updateOrgId();
        List<BloodMatchOutSheetVo> list = bloodMatchService.getApplyNum(bloodMatch,"0");
        model.addAttribute("list", list);
        model.addAttribute("bloodMatch", bloodMatch);
        model.addAttribute("bloodInpDetail", new BloodInpDetail());
        SysOrgDict sysOrgDict = new SysOrgDict();
        sysOrgDict.setType("BLOOD_MATCH_DICT");
        List<SysOrgDict> bloodMatchDict = sysOrgDictService.findList(sysOrgDict);
        model.addAttribute("bloodMatchDict", bloodMatchDict);
        return "modules/blood/bloodMatching";
	}

    /**
     * 根据申请单号获取患者信息
     * @param applyNum 申请单号
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getPatientInformation", ""})
    @ResponseBody
    public BloodMatchOutSheetVo getPatientInformation(String applyNum){
        BloodMatchOutSheetVo vo = bloodMatchService.getPatientInformation(applyNum, UserUtils.getOrgId(), "0");
        if(null != vo){
            vo.setSex(DictUtils.getDictLabel(vo.getSex(),"SEX_DICT",""));
            vo.setAppDept(deptDao.get(vo.getAppDept()).getDeptName());
            vo.setPatBloodGroup(DictUtils.getDictLabel(vo.getPatBloodGroup(),"BLOOD_TYPE_DICT",""));
            vo.setRh(DictUtils.getDictLabel(vo.getRh(),"rh_dict",""));
            vo.setChargeType(DictUtils.getDictLabel(vo.getChargeType(),"charge_type_dict",""));
            vo.setFastSlow(DictUtils.getDictLabel(vo.getFastSlow(),"FAST_SLOW",""));
        }
        return vo;
    }

    /**
     * 根据申请单号查询血液需求
     * @param applyNum 申请单号
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getBloodRequireByApplyNum", ""})
    public String getBloodRequireByApplyNum(String applyNum,Model model){
        List<BloodCapacity> list = bloodMatchService.getBloodRequireByApplyNum(applyNum, "0");
        model.addAttribute("list",list);
        return "modules/blood/bloodRequire";
    }

    /**
     * 加载血液库存信息
     * @param bloodInpDetail
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"loadBloodStock", ""}, method = RequestMethod.GET)
    public String loadBloodStock(BloodInpDetail bloodInpDetail, HttpServletRequest request, HttpServletResponse response, Model model){
        List<BloodInpDetail> list = bloodImportService.loadBloodStock(bloodInpDetail);
        model.addAttribute("stock",list);
        model.addAttribute("bloodInpDetail",new BloodInpDetail());
        return "modules/blood/bloodList";
    }

    /**
     * 根据ID获取血液入库明细记录
     * @param id
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getInpDetailById", ""})
    @ResponseBody
    public BloodMatch getInpDetailById(@Param("id") String id){
        BloodMatch match = bloodMatchService.getInpDetailById(id);
        match.setMatchMetchDictList(DictUtils.getDictList("BLOOD_WAY_DICT"));
        match.setMatchResultDictList(DictUtils.getDictList("BLOOD_RESULT_DICT"));
        List<User> users = UserUtils.getRoleUserList(UserUtils.getUser().getOrgDept() != null ? UserUtils.getUser().getOrgDept().getId() : "");
        match.setMatchUserList(users);
        return match;
    }

    /**
     * 配血单查询
     * @param bloodMatch
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"bloodMatchQuery",""})
    public String bloodMatchQuery(BloodMatch bloodMatch,HttpServletRequest request,HttpServletResponse response,Model model){
        List<BloodMatch> list = bloodMatchService.bloodMatchQuery(bloodMatch);
        model.addAttribute("list",list);
        return "modules/blood/bloodMatchQuery";
    }

    /**
     * 保存配血
     * @param bloodMatch
     * @param model
     * @return
     * @author fengyg
     * @date 2017-07-22
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
	@ResponseBody
	public StringData save(BloodMatch bloodMatch, Model model){
	    StringData data=new StringData();
		int i=bloodMatchService.saveBloodMatch(bloodMatch);
        if(i>0){
            data.setCode("success");
            data.setData("配血成功");
        }else{
            data.setCode("error");
            data.setData("配血失败");
        }
        return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(BloodMatch bloodMatch) {
	    StringData data=new StringData();
		int i=bloodMatchService.delete(bloodMatch);
        if(i>0){
            data.setCode("success");
            data.setData("删除配血功能成功");
        }else{
            data.setCode("error");
            data.setData("删除配血功能失败");
        }
		return data;
	}

}